Page({
  data: {
    listData: [],
  },
  async onLoad(ev) {
    console.log(ev)
    this.getListData(ev.q)
  },
  async getListData(q) {
    const {
      data: { results },
    } = await wx.http.get('/v1_0/search', {
      page: 1,
      per_page: 10,
      q,
    })
    this.timestampToYear(results)
  },
  timestampToYear(results) {
    // 获取当前时间
    const now = new Date()
    for (let item in results) {
      // console.log(results[item].pubdate);
      // 获取需求时间
      const demandTime = new Date(results[item].pubdate)
      // 计算时间差
      const temporal = now.getTime() - demandTime.getTime()
      // 计算年份差
      const diffYear = Math.floor(temporal / (1000 * 60 * 60 * 24 * 365))
      results[item].pubdate = diffYear
      // console.log(results[item].pubdate);
      // console.log(results[item]);
    }

    this.setData({
      listData: results,
    })
  },
})
